package com.cykj.admin.service.impl;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.cykj.admin.mapper.AdminMapper;
import com.cykj.admin.service.IAdminService;
import com.cykj.admin.vo.AdminVO;

import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.AdminDO;
import com.cykj.common.entity.UserDO;
import com.cykj.common.utils.JwtUtils;
import com.cykj.common.utils.SaTokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @BelongsProject:@BelonqsPackage:com.cykj.admin.service.impl
 * @Author:jiangna
 * @CreateTime:2025-08-15 10:39
 */
@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * @Description: 管理员登录
     * @Param: [adminVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO login(AdminVO adminVO) {
        //1.根据唯一标识 获取验证码答案
        String attribute = (String) redisTemplate.opsForValue().get(adminVO.getUnique());
        System.out.println(attribute);
        //2.获取用户输入的验证码
        String code = adminVO.getCode();

        //3.判断验证码是否正确忽略大小写判断
        if (!code.equalsIgnoreCase(attribute)) {
            return ResponseDTO.codeError("验证码错误!");
        }

        AdminDO adminDO = adminMapper.login(adminVO);
        if (adminDO == null) {
            return ResponseDTO.error("账号或密码错误，请重新登录");
        }
        if (adminDO.getStatus() != 0) {
            AdminDO superAdmin = adminMapper.selectSuperAdmin();
            return ResponseDTO.error("状态异常,请联系超级管理员,手机为" + superAdmin.getTelephone());
        }
        SaTokenUtils.saLogin("admin", adminDO.getAdminId());
        redisTemplate.opsForValue().set("admin:" + adminDO.getAdminId(), adminDO);
        return ResponseDTO.success("登录成功！");

    }


    /**
     * @Description: 查询所有管理员数据 / 根据查询条件查询管理员
     * @Param: [adminVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO selectAdminsByCondition(AdminVO adminVO) {
        PageInfo<AdminDO> page;
        //开启分页
        PageHelper.startPage(adminVO.getCurrentPage(), adminVO.getPageSize());
        List<AdminDO> adminList = adminMapper.selectAdminsByCondition(adminVO);
        System.out.println("list: " + adminList);
        page = new PageInfo<>(adminList);
        if (page.getTotal() < page.getPageSize() && adminList.size() == 0 && page.getTotal() > 0) {
            adminVO.setCurrentPage(1);
            PageHelper.startPage(adminVO.getCurrentPage(), adminVO.getPageSize());
            List<AdminDO> adminList2 = adminMapper.selectAdminsByCondition(adminVO);
            page = new PageInfo<>(adminList2);
        }
        return ResponseDTO.success(page);
    }

    /**
     * @Description: 重置管理员密码
     * @Param: [adminVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO resetPassword(Integer adminId) {
        int result = adminMapper.resetPassword(adminId);
        if (result > 0) {
            return ResponseDTO.success("密码重置成功");
        } else {
            return ResponseDTO.error("密码重置失败");
        }
    }

    /**
     * @Description: 拉黑管理员
     * @Param: [adminVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO deleteAdmin(AdminVO adminVO) {
        int result = adminMapper.deleteAdmin(adminVO);
        if (result > 0) {
            return ResponseDTO.success("操作成功!,已拉黑");
        }
        return ResponseDTO.error("拉黑失败，检查账号是否存在!");

    }

    /**
     * @Description: 获取管理员信息
     * @Param: []
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO getAdminInfo() {
        Object admin = redisTemplate.opsForValue().get("admin:" + StpUtil.getLoginIdAsInt());
        return ResponseDTO.success(admin);
    }

    /**
    * @Description: 退出登录
    * @Param: []
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO logout() {
        try {
            StpUtil.logout();
            return ResponseDTO.success("退出登录成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.error("退出登录失败，请稍后重试");
        }
    }
}
